﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.DynamicData;
using System.Web.Security;

namespace DDE
{
    public static class AttributeManager
    {
        /// <summary>
        /// Gets the specified attribute from the attribute collection
        /// </summary>
        /// <param name="attributes">Extends an Attribute collection</param>
        /// <param name="attributeType">The type of attribute to check for and return if found</param>
        /// <returns></returns>
        public static Attribute GetAttribute(this AttributeCollection attributes, Type attributeType)
        {
            foreach (Attribute attribute in attributes)
                if (attribute.GetType().Equals(attributeType))
                    return attribute;

            return null;
        }
    }

    public class CustomDynamicDataRouteHandler :
        DynamicDataRouteHandler
    {
        public CustomDynamicDataRouteHandler()
        { }


        /// <summary>
        ///  Role Verification
        /// </summary>
        /// <param name="route">Route Asked</param>
        /// <param name="table">Table asked</param>
        /// <param name="action">Action asked</param>
        /// <returns></returns>
        public override IHttpHandler CreateHandler(
            DynamicDataRoute route, MetaTable table, string action)
        {
            if (Roles.IsUserInRole("admin") || (action == "List"))
                return base.CreateHandler(route, table, action);
            return null;

        }
    }
}